<?php

/**
 * This plugin stores the current URL in session
 */
class Tomato_Controller_Plugin_UrlTracker extends Zend_Controller_Plugin_Abstract {
	
	/**
	 * Session key which is used to save the URL
	 * @var const
	 */
	const SESSION_NS = 'Tomato_Controller_Plugin_UrlTracker';
	
	/**
	 * Store the exclude actions that we don't want to store the URL
	 * @var array
	 */
	private $_exclude = array ();
	
	/**
	 * @param array $exclude Exclude actions
	 * Each exclude item is a string in the format of module_controller_action
	 */
	public function __construct($exclude = array()) {
		$this->_exclude = $exclude;
	}
	
	public function preDispatch(Zend_Controller_Request_Abstract $request) {
		/**
		 * Combine the current module, controller and action
		 */
		$mca = implode ( '_', array ($request->getModuleName (), $request->getControllerName (), $request->getActionName () ) );
		if (! in_array ( $mca, $this->_exclude )) {
			$session 				= new Zend_Session_Namespace ( self::SESSION_NS );
			$session->previous_url 	= (isset($session->temp_url)) ? $session->temp_url : false;
			$session->temp_url 		= (isset($session->url)) ? $session->url : "";
			$session->url 			= $request->getRequestUri ();			
			$session->isXhr 		= $request->isXmlHttpRequest ();
		}
	}
}
